﻿using DesignerHelper.Main;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DesignerHelper.Help;
using System.IO;
using System.Linq;
using System.ComponentModel;
using DesignerHelper.Commands;
using DesignerHelper.EventHandlers;
using WorkbookDesigner.Addons.DefineName;
using WorkbookDesigner.UI;


namespace WorkbookDesigner.Main
{
    public partial class WorkBookForm : Form, IBookForm
    {
        public WorkBookForm(IWorkbook workbook, IMainWindow mdiParent) : this(mdiParent)
        {
            presenter.SetActiveBook(workbook);
        }
        private WorkBookForm(IMainWindow mw)
        {
            InitializeComponent();
            presenter = new BookFormPresenter(mw, this);
            MdiParent = mw.GetForm();
        }


        IMainWindow IBookForm.ParentForm => presenter.MdiParent;

        public WorkbookView WorkbookView => workbookview;

        public IWorkbook ActiveBook
        {
            get { return WorkbookView.ActiveWorkbook; }
            private set { WorkbookView.ActiveWorkbook = value; }
        }

        public IWorksheet ActiveWorkSheet
        {
            get { return WorkbookView.ActiveWorksheet; }
            private set { WorkbookView.ActiveWorksheet = value; }
        }

        Form IBookForm.GetForm()
        {
            return this;
        }

        public bool HasPageBreaks
        {
            get
            {
                IWorksheet sheet = WorkbookView.ActiveWorksheet;
                IVPageBreaks vb = sheet.VPageBreaks;
                IHPageBreaks hb = sheet.HPageBreaks;
                return vb.Count != 0 || hb.Count != 0;
            }

        }


        private BookFormPresenter presenter;

        public IBookEditor Editor => presenter;

        #region WorkbookView相关操作方法


        public void ShowDefineNameDialog()
        {
        }

        public void ShowLocateRangeDialog()
        {
            presenter.ShowLocateRangeDialog();
        }

        #endregion WorkbookView相关操作方法

        #region File相关方法


        /// <summary>
        /// 将当前控件窗口中打开的工作簿文件另存为
        /// </summary>
        public void FileSaveAs()
        {
            presenter.FileSaveAs();
        }


        /// <summary>
        /// 保存当前控件窗口中打开的工作簿文件
        /// </summary>
        public void SaveFile()
        {
            presenter.SaveFile();
        }


        #endregion File相关方法





        #region 窗体事件

        private void ClearContents_Click(object sender, EventArgs e)
        {
            presenter.ClearRangeSelection(Edit.ClearType.Contents);
        }

        private void Copy_Click(object sender, EventArgs e)
        {
            presenter.EditCopy();
        }

        private void Cut_Click(object sender, EventArgs e)
        {
            presenter.EditCut();
        }

        private void DefineName_Click(object sender, EventArgs e)
        {
            presenter.ShowDefineNameDialog();
        }

        private void DeleteCells_Click(object sender, EventArgs e)
        {
            presenter.OpenDeleteCellsDialog();
        }

        private void InsertCells_Click(object sender, EventArgs e)
        {
            presenter.OpenInsertCellsDialog();
        }

        private void Paste_Click(object sender, EventArgs e)
        {
            presenter.EditPaste();
        }

        private void PasteSpecial_Click(object sender, EventArgs e)
        {
            presenter.OpenPasteSpecialDialog();
        }

        private void WorkBookForm_Deactivate(object sender, EventArgs e)
        {
            WorkbookView.EndEdit(); //结束工作表编辑
        }

        private void WorkBookForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            presenter.FormClosed(sender, e);
        }

        private void WorkBookForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            presenter.FormClosing(sender, e);
        }

        private void WorkBookForm_Load(object sender, EventArgs e)
        {

        }

        private void deleteComments_Click(object sender, EventArgs e)
        {
            Edit.ClearComment(this);
        }

        private void editComments_Click(object sender, EventArgs e)
        {
            Edit.EditComment(this);
        }

        private void InsertComments_Click(object sender, EventArgs e)
        {
            Edit.InsertComment(this);
        }

        private void menuItemDefineRangeSelection_Click(object sender, EventArgs e)
        {
            presenter.DefineNamesFromSelection();
        }

        private void menuItemLocateRange_Click(object sender, EventArgs e)
        {
            presenter.ShowLocateRangeDialog();
        }

        private void ShowComments_Click(object sender, EventArgs e)
        {
            workbookview.DoWork(() =>
            {
                WorkbookView.RangeSelection.Comment.Visible = !WorkbookView.RangeSelection.Comment.Visible;
            });
        }


        #endregion 窗体事件

        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            WorkbookView wbv = this.workbookview;
            wbv.DoWork(() =>
            {
                IRange rangeSelection = wbv.RangeSelection;
                if (rangeSelection.CellCount > 1)
                {
                    toolStripMenuItemAboutComment.Enabled = false;
                    return;
                }
                toolStripMenuItemAboutComment.Enabled = true;
                showComments.Enabled = true;
                var comment = rangeSelection.Comment;//Main?
                deleteComments.Enabled
                    = editComments.Enabled
                        = showComments.Enabled = comment != null;
                if (comment == null)
                    return;

                showComments.Text = comment.Visible ? "隐藏批注" : "显示批注";

            });

        }

        private void workbookview_CellEndEdit(object sender, CellEndEditEventArgs e)
        {
            presenter.HightLightTheCell(e);
        }
    }
}